﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DigitalHorizon
{
	/// <summary>
	/// Interaction logic for DigitalHorizonInsturment.xaml
	/// </summary>
	public partial class DigitalHorizonInsturment : UserControl
	{
		/// <summary>Dependency property to Get/Set the current PitchAngle</summary>
        public static readonly DependencyProperty PitchAngleProperty =
            DependencyProperty.Register("PitchAngle", typeof(double), typeof(DigitalHorizonInsturment),// null);
                new PropertyMetadata(new PropertyChangedCallback(DigitalHorizonInsturment.PitchAnglePropertyChanged)));
		
		/// <summary>Dependency property to Get/Set the current PitchAngle</summary>
        public static readonly DependencyProperty BankAngleProperty =
            DependencyProperty.Register("BankAngle", typeof(double), typeof(DigitalHorizonInsturment),// null);
                new PropertyMetadata(new PropertyChangedCallback(DigitalHorizonInsturment.BankAnglePropertyChanged)));
       
        /// <summary>Gets/Sets Value</summary>
        public double PitchAngle
        {
            get { return (double)GetValue(PitchAngleProperty); }
            set
            {
                SetValue(PitchAngleProperty, value);
            }
        }        
		/// <summary>Gets/Sets Value</summary>
        public double BankAngle
        {
            get { return (double)GetValue(BankAngleProperty); }
            set
            {
                SetValue(BankAngleProperty, value);
            }
        }

        private static void PitchAnglePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            DigitalHorizonInsturment horizon = (DigitalHorizonInsturment)d;
            double newValue = (double)e.NewValue;
            double oldValue = (double)e.OldValue;
			/// <summary>
			/// -100 =-90 degrees
			///  100 = 90 degrees
			/// Original PitchAngleLines
			/// From -100 to 100 = 1000 pixels
			/// 1 degree = 50 pixels
			/// PitchAngleLines used in DigitalHorizonInstrument is stretched by a factor of 4
			/// From -100 to 100 = 4000 pixels
			/// 1 degree = 200 pixels
			/// Position at 0 degree = -1500
			/// Position at 90 degrees=-3300
			/// Position at-90 degrees=  300
			/// formular: 18x-1500 for translate
			/// 18: factor from the -100 to 100 in -90 to 90 is 0.9 and 1 degrees is 20 pixels. so 0.9*20 = 18
			/// </summary>
			double positionHorizontalLines = 18*newValue-1500;
			Canvas.SetTop(horizon.HorizontalLines,positionHorizontalLines);
			
			double positionSkyPanel = 18*newValue-920;
			if(positionSkyPanel>-210){
				positionSkyPanel = -210;
			}else if(positionSkyPanel<-1630){
				positionSkyPanel=-1630;
			}
			Canvas.SetTop(horizon.SkyPanel,positionSkyPanel);
        }

        private static void BankAnglePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            DigitalHorizonInsturment horizon = (DigitalHorizonInsturment)d;
            double newValue = (double)e.NewValue;
            double oldValue = (double)e.OldValue;
			RotateTransform rotation = new RotateTransform(newValue,horizon.InstrumentCanvas.ActualWidth/2, horizon.InstrumentCanvas.ActualHeight/2);
			horizon.InstrumentCanvas.RenderTransform = rotation;
			
		}
		
		public DigitalHorizonInsturment()
		{
			this.InitializeComponent();
			RootCanvas.ClipToBounds = true;
		}
	}
}